/**
 * 
 */
package com.wixc.controller;

import huanqiu.HuanQiu_SocietyNews;
import ifeng.IFeng_News;
import ifeng.IFeng_ScienceNews;

import java.io.FileNotFoundException;
import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import baidu.BaiDuZhiDaoRiBao;
import baidu.BaiduCircleAreaNews;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;
import com.wixc.util.JdbcUtils;

import cankaoxiaoxi.CanKaoXiaoXi_ChinaNews;
import cankaoxiaoxi.CanKaoXiaoXi_HotNews;
import zhihu.ZhiHu;

/**
 * @author weichuanxi
 * @date 2016年4月15日
 */
@Controller
@RequestMapping(value = "/")
public class RunController {
	private static Logger log = Logger.getLogger(RunController.class);

	@RequestMapping(value = "/zhihu_run")
	public ModelAndView ZhiHuRun(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("index");
		Connection con = JdbcUtils.getConnection();
		try {
			ZhiHu.ZhiHu_Run();
			BaiDuZhiDaoRiBao.BaiDu_Run();
			log.info("本次更新完毕");
			Statement state = (Statement) con.createStatement();
			state.execute("delete  from userbean where `password`=''");
			con.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}

	@RequestMapping(value = "/news_run")
	public ModelAndView NewsRun(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("temp");
		Connection con = JdbcUtils.getConnection();
		try {
			CanKaoXiaoXi_ChinaNews.CanKaoXaioXi_News_Run();
			CanKaoXiaoXi_HotNews.CanKaoXaioXi_HotNews_Run();
			HuanQiu_SocietyNews.HuanQiu_News_Run();
			IFeng_News.IFeng_ChinaNews_Run();
			IFeng_News.IFeng_ForeignNews_Run();
			IFeng_News.IFeng_SociaNews_Run();
			IFeng_ScienceNews.IFeng_ScienceNews_Run();
			BaiduCircleAreaNews.Baidu_HotNews_Run();
			log.info("本次更新完毕");
			log.info("删除重复记录...");
			Statement state = (Statement) con.createStatement();
			state.execute("delete from news where id not in(select * from (select max(id) from news group by title) as tmp)");
			state.execute("delete  from userbean where `password`=''");
			con.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return mv;
	}
}
